/*******************************************************************************
NETS_Span_of_Control_Data.do

This file processes NETS data for Tables IV, VII, and VIII. For M=3, 6, 12, 24, 
48, it computes total firm employment excluding the own firm contribution of 
employment in the location, as well as average employment and total number of 
plants, for each firm-location pair. For all M, it then repeat these steps using 
only non-imputed data.

Last updated: 6/7/2021
*******************************************************************************/

version 15
cd "C:\Plants_in_Space"
set more off
set type double
set varabbrev off
graph set window fontface "Times New Roman" 

use "Data\Intermediate\NETS\NETS_HQs_at_least_5_employees_2014_cleaned.dta", clear

/*******************************************************************************
For each M, compute for each firm-location average employment, number 
of plants, and the firm's total employment excluding its own contribution in the 
particular location. In the resulting data, each observation is a firm-location.
*******************************************************************************/
foreach m of numlist 3 6 12 24 48 {
	preserve
		// Recompute total firm employment to exclude its employment in the 
		// particular location
		by hq sic8 ID_`m', sort: egen firm_location_emp = total(emp)
		gen firm_emp_net_location = firm_employment - firm_location_emp

		// Compute average employment and number of plants for each firm-location
		by hq sic8 ID_`m', sort: egen firm_location_avg_emp = mean(emp)
		by hq sic8 ID_`m', sort: gen firm_location_num_estab = _N
		
		// Take logs of variables
		foreach var of varlist firm_emp_net_location firm_location_avg_emp firm_location_num_estab {
			gen log_`var' = log(`var')
		}

		// Generate fixed effects group
		egen industry_loc_group = group(sic8 ID_`m')
		
		// Keep unique observations
		by hq sic8 ID_`m', sort: keep if _n==1
		keep establishment hq sic8 sic2 emp emp_record_code ID_`m' firm_employment ///
			 log_firm_employment firm_location_emp firm_emp_net_location ///
			 firm_location_avg_emp firm_location_num_estab log_firm_emp_net_location ///
			 log_firm_location_avg_emp log_firm_location_num_estab industry_loc_group
		save "Data\Final\TablesIV_VII_VIII\span_of_control_M`m'_data.dta", replace
	restore
}

**********************Repeat, using non-imputed data only***********************
foreach m of numlist 3 6 12 24 48 {
	// Keep only non-imputed data, and redefine total firm employment (these were
	// calculated including the non-imputed data).
	keep if emp_record_code == 0
	drop firm_employment log_firm_employment
	by hq sic8, sort: egen firm_employment = total(emp)
	gen log_firm_employment = log(firm_employment)
	
	preserve				
		// Recompute total firm employment to exclude its employment in the 
		// particular location
		by hq sic8 ID_`m', sort: egen firm_location_emp = total(emp)
		gen firm_emp_net_location = firm_employment - firm_location_emp

		// Compute average employment and number of plants for each firm-location
		by hq sic8 ID_`m', sort: egen firm_location_avg_emp = mean(emp)
		by hq sic8 ID_`m', sort: gen firm_location_num_estab = _N
		
		// Take logs of variables
		foreach var of varlist firm_emp_net_location firm_location_avg_emp firm_location_num_estab {
			gen log_`var' = log(`var')
		}

		// Generate fixed effects group
		egen industry_loc_group = group(sic8 ID_`m')
		
		// Keep unique observations
		by hq sic8 ID_`m', sort: keep if _n==1
		keep establishment hq sic8 sic2 emp emp_record_code ID_`m' firm_employment ///
			 log_firm_employment firm_location_emp firm_emp_net_location ///
			 firm_location_avg_emp firm_location_num_estab log_firm_emp_net_location ///
			 log_firm_location_avg_emp log_firm_location_num_estab industry_loc_group
		save "Data\Final\TablesIV_VII_VIII\span_of_control_M`m'_nonimputed_data.dta", replace
	restore
}
